package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.Circle;
import cn.com.connext.scrm.service.CircleService;
import cn.com.connext.scrm.service.dto.CircleDTO;
import cn.com.connext.scrm.service.mapper.CircleMapper;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import com.codahale.metrics.annotation.Timed;
import com.connext.common.exception.ErrorCode;
import com.connext.common.exception.ServiceException;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URISyntaxException;
import java.util.Optional;
import java.util.concurrent.TimeoutException;

/**
 * Created by YANG205 on 2018/6/27.
 */
@RestController
@RequestMapping("/api/circle")
public class CircleResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(CircleResource.class);

    @Autowired
    private CircleService circleService;

    @Autowired
    private CircleMapper circleMapper;

    public CircleResource() throws TimeoutException {
        super("Circle");
    }

    /**
     * POST  /tasks : Create a new note.
     *
     * @param circleDTO the circleDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new circleDTO, or with status 400 (Bad Request) if the note has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/create")
    @Timed
    @ApiOperation(value = "创建圈子", tags = {"圈子列表"}, notes = "创建圈子")
    public ResponseEntity<CircleDTO> create(@Valid @RequestBody CircleDTO circleDTO) throws URISyntaxException {
        log.debug("REST request to save Circle : {}", circleDTO);
        if (StringUtils.isNotEmpty(circleDTO.getId())) {
            throw new ServiceException(ErrorCode.ERROR_ID_EXISTS);
        }
        Circle circle = circleMapper.toEntity(circleDTO);
        CircleDTO result = circleService.saveCircle(circle);

        return createSuccess(result);
    }

    /**
     * GET  /getCircleInfo/:id : get the "id" circle.
     *
     * @param id the id of the circleDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the circleDTO, or with status 404 (Not Found)
     */
    @GetMapping("/getCircleInfo/{id}")
    @Timed
    @ApiOperation(value = "获取圈子信息", tags = {"圈子列表"}, notes = "获取圈子信息")
    public ResponseEntity<CircleDTO> getCircleInfo(@PathVariable String id) {
        log.debug("REST request to get Circle : {}", id);
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException(ErrorCode.ERROR_ID_NOT_EXISTS);
        }
        CircleDTO circleDTO = circleService.findOneDTO(id);
        return entityResponse(Optional.ofNullable(circleDTO));
    }

    /**
     * DELETE  /delete/:id : delete the "id" circle.
     *
     * @param id the id of the circle to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/delete/{id}")
    @Timed
    @ApiOperation(value = "删除圈子", tags = {"圈子列表"}, notes = "删除圈子")
    public ResponseEntity deleteCircle(@PathVariable String id) {
        log.debug("REST request to delete Circle : {}", id);
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException(ErrorCode.ERROR_ID_NOT_EXISTS);
        }
        circleService.deleteCircle(id);
        return deleteSuccess();
    }

    /**
     * @param pageable
     * @return
     */
    @GetMapping("/circleList")
    @Timed
    @ApiOperation(value = "获取圈子列表", tags = {"圈子列表"}, notes = "获取圈子列表")
    public Page<CircleDTO> getCircleList(@ApiParam Pageable pageable) {
        CircleDTO circleDTO = new CircleDTO();
        return circleService.getAllCircles(pageable, circleDTO);
    }

//    @GetMapping("/reviewCircle/{id}")
//    @Timed
//    @ApiOperation(value = "审核圈子", tags = {"圈子列表"}, notes = "审核圈子")
//    public Boolean reviewCircle(@PathVariable String id) {
//        if (StringUtils.isEmpty(id)) {
//            throw new ServiceException(MyErrorCode.ERROR_ID_EMPTY);
//        }
//    }

}
